import java.util.*; // ArrayList, TreeSet

public class StudentAndStudentListTester {

    public static void main(String [] args) {

		ArrayList<Student> studentList = new ArrayList<Student>();

		TreeSet<Course> courseList = new TreeSet<Course>();

		// The course list must be created, from CourseList.txt, prior to
		// creating students from StudentList.txt.  This is because each Student
		// object contains a set of Course objects, one for each course in which
		// the student is registered.
		courseList = CourseList.readCourseList("CourseList.txt");
	
		// Create a student list, reading from StudentList.txt		
		studentList = StudentList.readStudentList("StudentList.txt", courseList);
	
		// Display info for each student in the list
		System.out.println("List of students and their courses:");
		for (Student s : studentList) {
			System.out.println(s.toString());
		}
		
		// Get the first student and register for another course.  When successful,
		// stop.
		Student testStudent = studentList.get(0);
		System.out.println("Attempting to register " + testStudent.getLogin() +
			" for another course...");
		for (Course c : courseList) {
			if (testStudent.registerCourse(c)== true) {
				break;
			}
		}
		
		// Create a new student and try to register for each course
		System.out.println("\nCreating a new student and registering for courses...");
		testStudent = new Student("NewStudentTest");
		StudentList.addStudent(testStudent, studentList);
		for (Course c : courseList) {
			testStudent.registerCourse(c);
		}
		
		// Display info for each student in the list
		System.out.println("\nUpdated list of students and their courses:");
		for (Student s : studentList) {
			System.out.println(s.toString());
		}
		
		// Write out a modified student file
		System.out.println("Writing updated student info to ModifiedStudentList1.txt");
		StudentList.writeStudentList(studentList, "ModifiedStudentList1.txt");

		// Now unregister the new student from all courses
		System.out.println("\nUnregistering new student from all courses...");
		for (Course c : courseList) {
			testStudent.unregisterCourse(c);
		}
			
		// Register the new student for a course, finding it by courseID
		System.out.println("\nFinding a course by ID and registering new student...");
		Course testCourse = CourseList.getCourse("CS003", courseList);
		testStudent.registerCourse(testCourse);

		// Display info for each student in the list
		System.out.println("\nUpdated list of students and their courses:");
		for (Student s : studentList) {
			System.out.println(s.toString());
		}

		// Look for students by login name
		System.out.println("\nSearching for students by login name...");
		String findName1 = "Toast";
		Student test1 = StudentList.getStudent(findName1, studentList);
		if (test1 != null) {
			System.out.println("Found student named " + test1.getLogin() + 
			" with coursees " + test1.getCourseIDs());
		} else {
			System.out.println("Did not find student with login: " + findName1);
		}
		String findName2 = "StudentA";
		Student test2 = StudentList.getStudent(findName2, studentList);
		if (test2 != null) {
			System.out.println("Found student named " + test2.getLogin() + 
			" with coursees " + test2.getCourseIDs());
		} else {
			System.out.println("Did not find student with login: " + findName2);
		}
		String findName3 = "student1";
		Student test3 = StudentList.getStudent(findName3, studentList);
		if (test3 != null) {
			System.out.println("Found student named " + test3.getLogin() + 
			" with coursees " + test3.getCourseIDs());
		} else {
			System.out.println("Did not find student with login: " + findName3);
		}
		String findName4 = "STUDENT1";
		Student test4 = StudentList.getStudent(findName4, studentList);
		if (test4 != null) {
			System.out.println("Found student named " + test4.getLogin() + 
			" with coursees " + test4.getCourseIDs());
		} else {
			System.out.println("Did not find student with login: " + findName4);
		}		
		// Write out a modified student file
		System.out.println("Writing updated student info to ModifiedStudentList2.txt");
		StudentList.writeStudentList(studentList, "ModifiedStudentList2.txt");

	}
}
